package org.kymjs.kjframe.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;

import java.util.List;

/**
 * Created by tse on 2015/12/12.
 */
public abstract class NumberCommonAdapter<T> extends BaseAdapter {
    protected Context mContext;
    protected List<T> mDatas;
    protected LayoutInflater mInflater;
    private int layoutId;
    private int showNumber;
    private final int tmpshowNumber;

    public NumberCommonAdapter(Context context, List<T> datas, int layoutId, int showNumber) {
        this.mContext = context;
        mInflater = LayoutInflater.from(context);
        this.mDatas = datas;
        this.layoutId = layoutId;
        this.showNumber = showNumber;
        this.tmpshowNumber = showNumber;
    }


    @Override
    public int getCount() {

        if (showNumber == -1 || showNumber > mDatas.size()) {
            return mDatas.size();

        } else {
            return showNumber;
        }
    }

    @Override
    public T getItem(int position) {
        return mDatas.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = ViewHolder.get(mContext, convertView, parent,
                layoutId, position);
        convert(holder, getItem(position), position);
        return holder.getConvertView();
    }

    public boolean toggle() {

        showNumber = (showNumber == -1) ? tmpshowNumber : -1;
        this.notifyDataSetChanged();
        return showNumber == -1;
    }


    public abstract void convert(ViewHolder holder, T t, int position);

}


